Perigrine Falcon Study¶

Falk Huettmann, University of Alaska, Fairbanks¶

Ernest Mack, Data Munging¶

This Jupyter notebook is located at: https://github.com/SeriousHyena/UAF-Temperature/blob/master/Perigrines.ipynb¶

Import the needed libraries¶

In [105]:
import folium
from folium import plugins
import seaborn as sns
import numpy as np
import pandas as pd
import geopandas as gpd
import pyproj
from shapely import geometry
import matplotlib.pyplot as plt

%matplotlib inline

Read in the Perigrines .csv file¶

In [106]:
Perigrines = pd.read_csv(r'C:\Users\emack\Documents\UAF-Temperature\data\pfunique2.csv') # save to a dataframe                   
Perigrines.head() # This shows the first 5 entries of the DF.
Out[106]:
longitude latitude
0 48.2282 -122.3490
1 44.5620 -68.8043
2 40.7545 -73.9645
3 40.4393 -73.9869
4 41.4992 -81.7147

Confirm the correct CRS¶

From the metadata we have that the geophysics data is referenced to latlong (NAD27) datum. Let's use pyproj to transform it to both latlong (WGS84) and UTM zone 16 (WGS84).

In [107]:
# North American Datum 1927
p1 = pyproj.Proj(proj='latlong', datum='NAD27')
# WGS84 Latlong
p2 = pyproj.Proj(proj='latlong', datum='WGS84')
# WGS84 UTM Zone 16
p3 = pyproj.Proj(proj='utm', zone=16, datum='WGS84')
Perigrines['long_wgs84'], Perigrines['lat_wgs84'] = pyproj.transform(p1, p2, 
                                                            Perigrines.longitude.values, 
                                                            Perigrines.latitude.values)
Perigrines['E_utm'], Perigrines['N_utm'] = pyproj.transform(p1, p3,
                                                        Perigrines.longitude.values,
                                                        Perigrines.latitude.values)

Convert our pandas DataFrame into a geopandas GeoDataFrame¶

In [108]:
Perigrines['geometry'] = [geometry.Point(x, y) for x, y in zip(Perigrines['long_wgs84'], Perigrines['lat_wgs84'])]

Pass the DataFrame to a GeoDataFrame object indicating which one is the geometry columns and the CRS (Coordinate Reference System):¶

In [109]:
Perigrines = gpd.GeoDataFrame(Perigrines, geometry='geometry', crs="+init=epsg:4326")

Save the file for re-use when we start working with other layers of data¶

In [110]:
Perigrines.to_csv(r'C:\Users\emack\Documents\UAF-Temperature\data\mag.csv')

Verify our new file looks ok¶

In [111]:
Perigrines.head(3)
Out[111]:
longitude latitude long_wgs84 lat_wgs84 E_utm N_utm geometry
0 48.2282 -122.3490 48.2282 -122.3490 inf inf POINT (48.228 -122.349)
1 44.5620 -68.8043 44.5620 -68.8043 2.273971e+06 -1.160805e+07 POINT (44.562 -68.804)
2 40.7545 -73.9645 40.7545 -73.9645 1.919624e+06 -1.111200e+07 POINT (40.755 -73.965)

.envelope is a shapely method to get the bounding rectangle of all the points in the geometry¶

In [112]:
multipoints = geometry.MultiPoint(Perigrines['geometry'])
bounds = multipoints.envelope

Let's save the bounds for the next parts of Integrating & Exploring.¶

In [113]:
gpd.GeoSeries(bounds).to_file(r'C:\Users\emack\Documents\UAF-Temperature\data\area_of_study_bounds.gpkg', 'GPKG')

Let's get the coordinates of the .boundary of the bounds.¶

In [114]:
coords = np.vstack(bounds.boundary.coords.xy)

Calculate the mean of the coordinates using numpy to center the location map on this.¶

In [167]:
Africa = (8.7832, 34.5085)
UAF_coords = (64.8558, -147.8335)
map_center = list(coords.mean(1))[::-1] # use a slice to get all the rows from the last column 
In [168]:
Africa
Out[168]:
(8.7832, 34.5085)

Area of high Perigrine Falcon Presences¶

In [171]:
m = folium.Map(location=Africa, zoom_start=3, control_scale=True)
folium.PolyLine(coords[::-1].T).add_to(m)
folium.LatLngPopup().add_to(m)
m
Out[171]:

Display the first 50 rows of Perigrine presence data¶

The 'long_wgs84 and lat_wgs84' columns confirm we have the correct CRS as they are the same as initial read from the .csv. The 'geometry' column is what we will use for later bounding-box zooming. the E_utm and N_utm are described here: https://en.wikipedia.org/wiki/Universal_Transverse_Mercator_coordinate_system

In [172]:
Perigrines.head(50)
Out[172]:
longitude latitude long_wgs84 lat_wgs84 E_utm N_utm geometry
0 48.22820 -122.34900 48.22820 -122.34900 inf inf POINT (48.228 -122.349)
1 44.56200 -68.80430 44.56200 -68.80430 2.273971e+06 -1.160805e+07 POINT (44.562 -68.804)
2 40.75450 -73.96450 40.75450 -73.96450 1.919624e+06 -1.111200e+07 POINT (40.755 -73.965)
3 40.43930 -73.98690 40.43930 -73.98690 1.923848e+06 -1.110263e+07 POINT (40.439 -73.987)
4 41.49920 -81.71470 41.49920 -81.71470 1.224469e+06 -1.057624e+07 POINT (41.499 -81.715)
5 44.43410 -73.25140 44.43410 -73.25140 1.903800e+06 -1.125511e+07 POINT (44.434 -73.251)
6 36.53490 -121.87800 36.53490 -121.87800 inf inf POINT (36.535 -121.878)
7 42.45500 -122.95000 42.45500 -122.95000 inf inf POINT (42.455 -122.950)
8 41.90630 -87.70200 41.90630 -87.70200 6.996664e+05 -1.015915e+07 POINT (41.906 -87.702)
9 -2.48677 -79.61160 -2.48677 -79.61160 1.660674e+06 -9.885857e+06 POINT (-2.487 -79.612)
10 41.34990 -72.97970 41.34990 -72.97970 1.994672e+06 -1.119832e+07 POINT (41.350 -72.980)
11 41.77310 -91.40100 41.77310 -91.40100 inf inf POINT (41.773 -91.401)
12 24.10510 -110.40500 24.10510 -110.40500 inf inf POINT (24.105 -110.405)
13 38.86910 -90.17160 38.86910 -90.17160 inf inf POINT (38.869 -90.172)
14 22.18810 -102.25700 22.18810 -102.25700 inf inf POINT (22.188 -102.257)
15 40.71060 -77.90480 40.71060 -77.90480 1.570132e+06 -1.083159e+07 POINT (40.711 -77.905)
16 48.54760 -122.48800 48.54760 -122.48800 inf inf POINT (48.548 -122.488)
17 48.20580 -122.36800 48.20580 -122.36800 inf inf POINT (48.206 -122.368)
18 17.97540 -94.50850 17.97540 -94.50850 inf inf POINT (17.975 -94.508)
19 -33.95792 18.45792 -33.95792 18.45792 6.828036e+06 3.224449e+06 POINT (-33.958 18.458)
20 58.07303 7.96696 58.07303 7.96696 4.602384e+06 1.892345e+07 POINT (58.073 7.967)
21 60.78749 11.08964 60.78749 11.08964 4.203573e+06 1.855224e+07 POINT (60.787 11.090)
22 49.42442 8.16521 49.42442 8.16521 5.818562e+06 1.875398e+07 POINT (49.424 8.165)
23 49.89280 -97.14850 49.89280 -97.14850 inf inf POINT (49.893 -97.148)
24 9.80015 -84.60560 9.80015 -84.60560 1.098887e+06 -1.006949e+07 POINT (9.800 -84.606)
25 14.57550 -90.51690 14.57550 -90.51690 inf inf POINT (14.576 -90.517)
26 53.88795 10.80935 53.88795 10.80935 5.121976e+06 1.846455e+07 POINT (53.888 10.809)
27 48.82964 12.96378 48.82964 12.96378 5.779495e+06 1.802323e+07 POINT (48.830 12.964)
28 49.82076 8.42048 49.82076 8.42048 5.754216e+06 1.872386e+07 POINT (49.821 8.420)
29 50.75470 -0.76543 50.75470 -0.76543 5.699457e+06 -1.988133e+07 POINT (50.755 -0.765)
30 40.68490 -124.21100 40.68490 -124.21100 inf inf POINT (40.685 -124.211)
31 26.15070 -97.17240 26.15070 -97.17240 inf inf POINT (26.151 -97.172)
32 26.25690 -97.38440 26.25690 -97.38440 inf inf POINT (26.257 -97.384)
33 37.26690 -111.77200 37.26690 -111.77200 inf inf POINT (37.267 -111.772)
34 26.60590 -78.49840 26.60590 -78.49840 1.681916e+06 -1.051799e+07 POINT (26.606 -78.498)
35 39.96010 -75.16230 39.96010 -75.16230 1.827430e+06 -1.100818e+07 POINT (39.960 -75.162)
36 39.16390 -74.71100 39.16390 -74.71100 1.882648e+06 -1.102081e+07 POINT (39.164 -74.711)
37 37.55380 -122.08800 37.55380 -122.08800 inf inf POINT (37.554 -122.088)
38 36.41980 -105.73100 36.41980 -105.73100 inf inf POINT (36.420 -105.731)
39 42.36410 -71.08660 42.36410 -71.08660 2.137406e+06 -1.136635e+07 POINT (42.364 -71.087)
40 45.50490 -73.60400 45.50490 -73.60400 1.850579e+06 -1.125301e+07 POINT (45.505 -73.604)
41 40.71380 -83.30160 40.71380 -83.30160 1.091933e+06 -1.045674e+07 POINT (40.714 -83.302)
42 28.17530 -82.66050 28.17530 -82.66050 1.242865e+06 -1.034810e+07 POINT (28.175 -82.660)
43 38.89540 -77.07180 38.89540 -77.07180 1.672157e+06 -1.085363e+07 POINT (38.895 -77.072)
44 18.46420 -66.11640 18.46420 -66.11640 3.134419e+06 -1.074927e+07 POINT (18.464 -66.116)
45 40.66980 -89.52540 40.66980 -89.52540 5.419431e+05 -1.003035e+07 POINT (40.670 -89.525)
46 42.27330 -83.69340 42.27330 -83.69340 1.045285e+06 -1.044475e+07 POINT (42.273 -83.693)
47 47.08130 -122.71800 47.08130 -122.71800 inf inf POINT (47.081 -122.718)
48 34.28180 -119.30800 34.28180 -119.30800 inf inf POINT (34.282 -119.308)
49 10.53860 -83.50250 10.53860 -83.50250 1.220633e+06 -1.009354e+07 POINT (10.539 -83.502)

Clean the data from Perigrines.csv and remove NaN values¶

In [173]:
Perigrines.dropna(how='any', inplace=True)
In [174]:
pd.isna(Perigrines)
Out[174]:
longitude latitude long_wgs84 lat_wgs84 E_utm N_utm geometry
0 False False False False False False False
1 False False False False False False False
2 False False False False False False False
3 False False False False False False False
4 False False False False False False False
... ... ... ... ... ... ... ...
24243 False False False False False False False
24244 False False False False False False False
24245 False False False False False False False
24246 False False False False False False False
24247 False False False False False False False

24247 rows × 7 columns

Plot a heatmat indicating Pergrine presences¶

In [175]:
# convert to (n, 2) nd-array format for heatmap
stationArr = Perigrines[['latitude', 'longitude']].as_matrix()

# plot heatmap
m.add_children(plugins.HeatMap(stationArr, radius=15))
m
C:\Users\emack\AppData\Local\Continuum\anaconda3\lib\site-packages\ipykernel_launcher.py:2: FutureWarning: Method .as_matrix will be removed in a future version. Use .values instead.
  
C:\Users\emack\AppData\Local\Continuum\anaconda3\lib\site-packages\ipykernel_launcher.py:5: FutureWarning: Method `add_children` is deprecated. Please use `add_child` instead.
  """
Out[175]:
In [176]:
# mark each station as a point
for index, row in Perigrines.iterrows():
    folium.CircleMarker([row['latitude'], row['longitude']],
                        radius=15,
                        #popup=row['name'],
                        fill_color="#3db7e4", # divvy color
                       ).add_to(m)

Plot heatmap with radial boundaries¶

In [177]:
# convert to (n, 2) nd-array format for heatmap
stationArr = Perigrines[['latitude', 'longitude']].as_matrix()

# plot heatmap
m.add_children(plugins.HeatMap(stationArr, radius=8))
m
C:\Users\emack\AppData\Local\Continuum\anaconda3\lib\site-packages\ipykernel_launcher.py:2: FutureWarning: Method .as_matrix will be removed in a future version. Use .values instead.
  
C:\Users\emack\AppData\Local\Continuum\anaconda3\lib\site-packages\ipykernel_launcher.py:5: FutureWarning: Method `add_children` is deprecated. Please use `add_child` instead.
  """
Out[177]:
In [ ]: